home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / UNIX.ZIP / VMSCRACK / GETOPT.C < prev    next >
C/C++ Source or Header  |  1996-11-14  |  1KB  |  102 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4.  
  5. #define SEPCHAR1 '-'
  6.  
  7. int optind=1;
  8. char *optarg;
  9.  
  10. static char *p=NULL;
  11.  
  12. int getopt(int argc,char *argv[],char *opt)
  13. {
  14.   unsigned char ch;
  15.   char *opt_ch;
  16.  
  17.   if (argc>optind) {
  18.  
  19.     if (p==NULL) {
  20.  
  21.       if ((p=argv[optind])==NULL || (*(p++)!=SEPCHAR1)) {
  22.  
  23.         optarg=p=NULL;
  24.  
  25.         return EOF;
  26.       }
  27.  
  28.       if (*p==SEPCHAR1) {
  29.  
  30.     optind++;
  31.         optarg=p=NULL;
  32.  
  33.         return EOF;
  34.       }
  35.  
  36.       if ((ch=*(p++))=='\0') {
  37.  
  38.     optind++;
  39.         optarg=p=NULL;
  40.  
  41.         return EOF;
  42.       }
  43.  
  44.       if ((ch==':') || ((opt_ch=strchr(opt,ch))==NULL)) {
  45.  
  46.     optarg=NULL;
  47.     errno=EINVAL;
  48.     perror("get command line option");
  49.         putchar('\n');
  50.  
  51.     return('?');
  52.       }
  53.  
  54.       if ((*(++opt_ch)==':')  || (*opt_ch=='*')) {
  55.  
  56.     optind++;
  57.  
  58.     if (*p=='\0') {
  59.  
  60.           if (*opt_ch!='*') {
  61.  
  62.         if (argc<=optind) {
  63.  
  64.           optarg=NULL;
  65.           errno=EINVAL;
  66.           perror("get command line option");
  67.               putchar('\n');
  68.  
  69.           return('?');
  70.             }
  71.  
  72.         p=argv[optind++];
  73.  
  74.           }
  75.           else
  76.             p=NULL;
  77.         }
  78.  
  79.     optarg=p;
  80.     p=NULL;
  81.  
  82.       }
  83.       else {
  84.  
  85.     if (*p=='\0') {
  86.  
  87.       optind++;
  88.       p=NULL;
  89.     }
  90.  
  91.     optarg = NULL;
  92.       }
  93.  
  94.       return ch;
  95.     }
  96.   }
  97.  
  98.   optarg=p=NULL;
  99.  
  100.   return EOF;
  101. }
  102.